//    Copyright (C) Kherty.  All rights reserved.

using System;
using System.Collections;
using System.Collections.Generic;

namespace OpenLS.Spreadsheet
{
    using TKey = Int32;
    class StableIntSortedDictionary<TValue>: IEnumerable<KeyValuePair<TKey, TValue>>
    {
        readonly Dictionary<TKey, TValue> dic = new Dictionary<TKey, TValue>();
        List<TKey> keysCache = null;

        private List<TKey> KeysCache
        {
            get
            {
                if (keysCache == null)
                {
                    keysCache = new List<TKey>();
                    foreach (TKey key in dic.Keys)
                        keysCache.Add(key);
                    keysCache.Sort();

                }
                return keysCache;
            }

        }

        private bool cacheValid;
        private int lastKey;
        private TValue lastValue;

        public int Count { get { return dic.Count; } }
        public void Add(TKey key, TValue value)
        {

            dic.Add(key, value); ForgetKeys();
            cacheValid = false;
            lastValue = default(TValue);
        }

        private void ForgetKeys()
        {
            keysCache = null;
        }
        public bool TryGetValue(TKey key, out TValue value)
        {
            if (cacheValid && key == lastKey)
            {
                value = lastValue;
                return true;
            }
            bool result =  dic.TryGetValue(key, out value);
            if (result)
            {
                cacheValid = true;
                lastValue = value;
                lastKey = key;
            }
            return result;
        }

        public void Remove(TKey key) { dic.Remove(key); ForgetKeys(); }
        public TValue this[TKey key] { get { return dic[key]; }
            set {
                ForgetKeys();
                dic[key] = value;
                cacheValid = false;
                lastValue = default(TValue);
            }
        }
        public IEnumerable<TValue> Values { get { foreach (TKey key in KeysCache)yield return dic[key]; } }
        public IEnumerable<TKey> Keys { get { return KeysCache; } }
        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumeratorImp()
        {
            foreach (TKey k in KeysCache)
                yield return new KeyValuePair<TKey, TValue>(k, dic[k]);
        }

        public bool ContainsKey(TKey key)
        {
            return dic.ContainsKey(key);
        }
        #region IEnumerable<KeyValuePair<TKey,TValue>> Members

        IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
        {
            return GetEnumeratorImp();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumeratorImp();
        }

        #endregion
    }
}
